<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 前台评论模块
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-03-03 09:31:20 +0800 (一, 2014-03-03) $
 * @version : $Id: comment_model.php 39 2014-03-03 01:31:20Z spike $
 * @rev     : $Revision: 39 $
 */
class Comment_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [获得一个评论]
     * @param  [string]     $comment_id
     * @param  [bool]       $enable        [是否排除"关闭状态"]
     * @return [array/false]
     */
    public function get_comment($comment_id, $enable = TRUE)
    {
        if( ! validate($comment_id, 'COMMENT_ID') )
        {
            $this->set_error("获取评论失败：评论ID格式不正确。[{$comment_id}]");
            return FALSE;
        }
        
        $where = array(
            'comment_id' => $comment_id,
            'is_delete'  => UNDELETED,
        );
        if($enable === TRUE)
        {
            $where['status'] = COMMENT_STATUS_ON;
        }

        $this->load->dao('Comment_dao');
        $comment = $this->Comment_dao->get_one($where);
        if( ! $comment)
        {
            $this->set_error("获取评论失败：无法找到评论。[{$comment_id}]");
            return FALSE;
        }

        return $comment;
    }


    // --------------------------------------------------------------------

    /**
     * [分页浏览]
     * @param  [array]  $params  [条件]
     * @param  [int]    $page    [当前页]
     * @param  [int]    $size    [页数，只允许app_config中"pagesize"的值]
     * @param  [string] $orderby [排序]
     * @return [array]
     */
    public function browse_comment($params, $page = NULL, $size = NULL, $orderby = '`comment_id` DESC')
    {
        $output = array(
            'size' => 0,
            'data' => array(),
            'total'=> 0,
        );

        $where = array(
            'is_delete' => UNDELETED,
        );
        $where_in = NULL;

        if( ! empty($params['nickname']) )
        {
            $where['`nickname` LIKE'] = "%".xss_clean(trim($params['nickname']))."%";
        }

        if( ! empty($params['comment']) )
        {
            $where['`comment` LIKE'] = "%".xss_clean(trim($params['comment']))."%";
        }

        if( ! empty($params['status']) )
        {
            $comment_status = $this->config->item('comment_status');
            if( $comment_status["{$params['status']}"] )
                $where['status'] = $params['status'];
        }
      
        if( ! empty($params['create_time_start']) && strtotime($params['create_time_start']) )
        {
            $where['`create_time` >'] = $params['create_time_start'];
        }

        if( ! empty($params['create_time_end']) && strtotime($params['create_time_end']) )
        {
            $where['`create_time` <'] = $params['create_time_end'];
        }

        if( ! empty($params['update_time_start']) && strtotime($params['update_time_start']) )
        {
            $where['`update_time` >'] = $params['update_time_start'];
        }

        if( ! empty($params['update_time_end']) && strtotime($params['update_time_end']) )
        {
            $where['`update_time` <'] = $params['update_time_end'];
        }
        
        $pagesize = $this->config->item('pagesize');
        $size     = isset($pagesize[$size]) ? $size : 10;
        $offset   = intval(($page-1)*$size) > 0 ? intval(($page-1)*$size) : 0;
        $total    = 0;

        $this->load->dao('Comment_dao');
        $data = $this->Comment_dao->get_in(
            $where_in,
            $where, 
            '*', 
            $orderby, 
            $size, 
            $offset, 
            TRUE, 
            $total
        );

        $output['size']  = $size; 
        $output['data']  = $data; 
        $output['total'] = $total;

        return $output;
    }

    // --------------------------------------------------------------------
    
    /**
     * [改变评论状态]
     * 
     * @param  [string]  $goods_sn   [编码]
     * @param  [string]  $act        [操作类型]
     * @return [bool]
     */
    public function change_comment_status($comment_id, $act)
    {   
        if( ! $comment = $this->get_comment($comment_id, FALSE))
        {
            $this->set_error("修改评论状态失败：无法找到评论[{$comment_id}]。");
            return FALSE;
        }

        // 初始化数据
        $_status       = NULL;  //修改的状态
        $_allow_status = NULL;  //允许修改的状态
        $_update       = NULL;  //更新的数据
        
        switch (strtolower(trim($act)))
        {
            case 'on':
                    $_status       = COMMENT_STATUS_ON;
                    $_allow_status = array(COMMENT_STATUS_OFF,COMMENT_STATUS_WAIT);
                    $_update       = array(
                        'status'       => $_status,
                        'update_time'  => now_str(),
                        'update_admin' => login_info('admin_id'), 
                    );
                break;

            case 'audit':
                    $_status       = COMMENT_STATUS_ON;
                    $_allow_status = array(COMMENT_STATUS_WAIT);
                    $_update       = array(
                        'status'       => $_status,
                        'update_time'  => now_str(),
                        'update_admin' => login_info('admin_id'), 
                    );
                break;

            case 'off':
                    $_status       = COMMENT_STATUS_OFF;
                    $_allow_status = array(COMMENT_STATUS_ON,COMMENT_STATUS_WAIT);
                    $_update       = array(
                        'status'       => $_status,
                        'update_time'  => now_str(),
                        'update_admin' => login_info('admin_id'), 
                    );
                break;

            default:
                    $this->set_error("修改评论状态失败：未知的操作[{$act}]。");
                    return FALSE;
                break;
        }

        $status      = $this->config->item('comment_status');
        $status_name = (string)$status[$_status];

        // 验证状态
        $current_status = (int)$comment['status'];
        if($current_status === $_status)
        {
            $this->set_error("修改评论状态失败：无需修改。");
            return FALSE;
        }

        if( $_allow_status && ! in_array($current_status, $_allow_status))
        {
            $this->set_error("修改评论状态失败：不允许被修改为[{$status_name}]状态。");
            return FALSE;
        }

       
        // 只有符合条件才会被修改
        $this->load->dao('Comment_dao');
        $affected_row = $this->Comment_dao->update_in(
            $_update,
            array('status'     => $_allow_status),
            array('comment_id' => $comment_id)
            );
        if($affected_row<=0)
        {
            $this->set_error("修改评论状态失败：无法写入数据库。");
            return FALSE;
        }

        return TRUE;
    }

    // --------------------------------------------------------------------
}
